#include "view_weather.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "Motor.h"
#include "view.h"
#include "times/view_times.h"
#include "sports_mode.h"
#include "font_mngr.h"
#include "app_trace.h"
#include "times/view_time_0.h"
#include "view_weather.inc"
#include "weather_ctrl.h"
#include "math.h"
#define LOG(fmt, ...) //app_trace_log(fmt "\r\n", ##__VA_ARGS__)

void view_weather_base_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
        weather_record_t weather = {0};
        weather_ctrl_get(0, &weather); //获取天气
        if (weather.date_index == 0)
        {
            disp_ctrl.com_data.i_temp[22] = 0;
        }
        else
        {
            disp_ctrl.com_data.i_temp[22] = 1;
        }
        disp_ctrl.com_data.i_temp[21] = weather_get_temperature_type();
        //如果是华氏度需要转换
        if (disp_ctrl.com_data.i_temp[21] != WEATHER_TEMPERATURE_TYPE_C)
        {
            weather.temperature_min = round(32 + weather.temperature_min * 1.8);
            weather.temperature_max = round(32 + weather.temperature_max * 1.8);
        }
        disp_ctrl.com_data.i_temp[18] = weather.type;
        disp_ctrl.com_data.i_temp[19] = weather.temperature_min;
        disp_ctrl.com_data.i_temp[20] = weather.temperature_max;
    }
    if (disp_ctrl.com_data.i_temp[5])
    {
        disp_draw_user_image(42, 3, img_ble_connect);
    }
    else
    {
        disp_draw_user_image(42, 0, img_ble_disconnect);
    }
    show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);
    disp_draw_user_image(23, 148, bmp_separator_lines);
    int weather_x = 23;
    int weather_y = 21;
    int weather_bmp_index = FILE_IMG_W106_noweather_23_21;
    switch (disp_ctrl.com_data.i_temp[18])
    {
    case WEATHER_TYPE_SUNSHINE:
        weather_bmp_index = FILE_IMG_W101_sun_23_21;
        break;
    case WEATHER_TYPE_RAINY:
        weather_bmp_index = FILE_IMG_W102_rain_23_21;
        break;
    case WEATHER_TYPE_SNOW:
        weather_bmp_index = FILE_IMG_W103_snow_23_21;
        break;
    case WEATHER_TYPE_HAZE:
        weather_bmp_index = FILE_IMG_W104_mist_23_21;
        break;
    case WEATHER_TYPE_CLOUDY:
        weather_bmp_index = FILE_IMG_W105_cloudy_23_21;
        break;
    }
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, weather_x, weather_y, weather_bmp_index);
    int const slash_x = 110;
    int const slash_y = 177;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, slash_x, slash_y, FILE_IMG_W110_num_line_20x44);
    int x = 0;
    int num_y = 177;
    int const unit_width = 20;
    int num_width = 20;
    int num_height = 44;
    int const num_interval = 8;
    int const unit_interval = 1;
    int const slash_interval = 8;
    int const slash_width = 20;
    int const negative_height = 3;
    int const negative_y = num_y + (num_height - negative_height) / 2;

    int min_temperature = disp_ctrl.com_data.i_temp[19];
    int max_temperature = disp_ctrl.com_data.i_temp[20];
    if (weather_bmp_index == FILE_IMG_W106_noweather_23_21 || min_temperature < -100 || min_temperature > 200 || max_temperature < -100 || max_temperature > 200 || max_temperature < min_temperature)
    {
        // -- / --
        x = slash_x - slash_interval - num_width * 2 - num_interval;
        disp_draw_rect_color(x, negative_y, x + num_width - 1, negative_y + negative_height - 1, RGB565_COLOR_WHITE);
        x += num_interval + num_width;
        disp_draw_rect_color(x, negative_y, x + num_width - 1, negative_y + negative_height - 1, RGB565_COLOR_WHITE);
        x = slash_x + slash_width + slash_interval;
        disp_draw_rect_color(x, negative_y, x + num_width - 1, negative_y + negative_height - 1, RGB565_COLOR_WHITE);
        x += num_interval + num_width;
        disp_draw_rect_color(x, negative_y, x + num_width - 1, negative_y + negative_height - 1, RGB565_COLOR_WHITE);
        return;
    }

    // c /
    int unit_index = FILE_IMG_W108_unit_c_75_171;
    if (disp_ctrl.com_data.i_temp[21] != WEATHER_TEMPERATURE_TYPE_C)
    {
        unit_index = FILE_IMG_W107_unit_f_75_171;
    }
    x = slash_x - slash_interval - unit_width;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, slash_y, unit_index);

    // - 100 c /
    int digits = get_num_digit(min_temperature);
    x -= unit_interval + num_width * digits + num_interval * (digits - 1);
    if (min_temperature < 0)
    {
        disp_draw_rect_color(x, negative_y, x + num_width - 1, negative_y + negative_height - 1, RGB565_COLOR_WHITE);
        x += num_width + num_interval;
    }
    disp_draw_user_image_num(x, num_y, min_temperature, num_width + num_interval, num_20x44_TAB);

    // - 100 c / - 100 c
    x = slash_x + slash_width + slash_interval;
    if (max_temperature < 0)
    {
        disp_draw_rect_color(x, negative_y, x + num_width - 1, negative_y + negative_height - 1, RGB565_COLOR_WHITE);
        x += num_width + num_interval;
    }
    x = disp_draw_user_image_num(x, num_y, max_temperature, num_width + num_interval, num_20x44_TAB);
    x -= num_interval - unit_interval;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, slash_y, unit_index);
}
void view_weather_display(int view)
{
    view_weather_base_display();
}
view_event_result_t view_weather_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
